package com.xncoding.socket.socket.client.javax;

import javax.websocket.*;
import java.nio.ByteBuffer;

@ClientEndpoint
public class JavaxWebSocketClientEndpoint {

    @OnOpen
    public void onOpen(Session session) {
        //连接建立
    }

    @OnClose
    public void onClose(Session session, CloseReason reason) {
        //连接关闭
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        //接收文本消息
        session.getAsyncRemote().sendText(message);
    }

    @OnMessage
    public void onMessage(Session session, PongMessage message) {
        //接收pong消息
        //发送对象消息，会尝试使用Encoder编码
        session.getAsyncRemote().sendObject(message);
    }

    @OnMessage
    public void onMessage(Session session, ByteBuffer message) {
        //接收二进制消息
        //发送二进制消息
        session.getAsyncRemote().sendBinary( message);
    }

    @OnError
    public void onError(Session session, Throwable e) {
        //异常处理
    }

    //发送ping
//session.getAsyncRemote().sendPing(ByteBuffer buffer);
//发送pong
//session.getAsyncRemote().sendPong(ByteBuffer buffer);
}